package com.yihaodian.search.code.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.yihaodian.api.search.query.TermQuery;
import com.yihaodian.search.code.model.Attribute;
import com.yihaodian.search.code.model.CodeSearchRequest;
import com.yihaodian.search.code.model.Item;
import com.yihaodian.search.code.model.SearchResult;
import com.yihaodian.search.code.model.SourceHit;
import com.yihaodian.search.code.service.SearchCodeService;
import com.yihaodian.search.code.service.impl.SearchCodeServiceImpl;

public class SearchCodeAction extends ActionSupport{
	/**
	 * TODO:
	 * 1. Shard by projectName
	 * 2. Auto complete
	 */
	// Search keyword
	private String keyword;
	
	private String uid;
	
	private SourceHit hit;
	
	private SearchResult result;
	
	private List<String> fp;
	
	private List<String> ff;
	
	private Attribute projectAttr;
	
	private Attribute fileTypeAttr;
	
	private int pageNo;
	
	private int sortBy;
	
	private SearchCodeService searchService = new SearchCodeServiceImpl();
	
	/**
	 * The main page for code search
	 * @return
	 */
	public String searchCode() {
		CodeSearchRequest request = new CodeSearchRequest();
		request.setKeyword(keyword);
		request.setPageNo(pageNo);
		request.setSortBy(sortBy);
		
		projectAttr = convertToAttribute("project", fp);
		fileTypeAttr = convertToAttribute("fileType", ff);
		request.setProjectAttr(projectAttr);
		request.setFileTypeAttr(fileTypeAttr);
		result = searchService.search(request);
		return SUCCESS;
	}
	
	private Attribute convertToAttribute(String type, List<String> stringList){
		List<Item> items = new ArrayList<Item>();
		if (stringList != null && stringList.size() > 0) {
			for (String element : stringList) {
				String array[] = element.split(",");
				Item item = new Item(array[0], array[1], Boolean.parseBoolean(array[2]));
				items.add(item);
			}
		}
		
		return new Attribute(type, items);
	}
	
	/**
	 * A page to show the full text of source files.
	 * @return
	 */
	public String showDetail() {
		hit = searchService.fetchSource(uid);
		return SUCCESS;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public SearchResult getResult() {
		return result;
	}

	public void setResult(SearchResult result) {
		this.result = result;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public void setHit(SourceHit hit) {
		this.hit = hit;
	}

	public SourceHit getHit() {
		return hit;
	}

	public void setFp(List<String> fp) {
		this.fp = fp;
	}

	public Attribute getProjectAttr() {
		return projectAttr;
	}

	public void setFf(List<String> ff) {
		this.ff = ff;
	}

	public Attribute getFileTypeAttr() {
		return fileTypeAttr;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getSortBy() {
		return sortBy;
	}

	public void setSortBy(int sortBy) {
		this.sortBy = sortBy;
	}
}
